
#include "addcarddialog.h"

#include "sqlite3stmt.h"

/* 
 *  Constructs a AddCardDialog which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 *
 *  The dialog will by default be modeless, unless you set 'modal' to
 *  TRUE to construct a modal dialog.
 */
AddCardDialog::AddCardDialog( sqlite3* s, QWidget* parent,  const char* name, bool modal, Qt::WFlags fl )
    : QDialog( parent, name, modal, fl ), sql(s)
{
  setupUi(this);
}

/*  
 *  Destroys the object and frees any allocated resources
 */
AddCardDialog::~AddCardDialog()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void AddCardDialog::languageChange()
{
    retranslateUi(this);
}

/*
 * public slot
 */
void AddCardDialog::do_search()
{
  QString q = "SELECT card_t.name FROM card_t WHERE card_t.name LIKE '%"+leSearchText->text().replace('\'',"''")+"%';";

  lbPrintings->setEnabled(false);
  bOK->setEnabled(false);

  lbResults->clear();
  
  for(sqlite3Stmt s(sql,q) ; !s.done() ; s.step()) {
    lbResults->insertItem(s.column_text(0));
  }

  lbResults->setEnabled(true);

}

/*
 * public slot
 */
void AddCardDialog::result_selected(const QString& ri)
{
  QString r = ri;
  QString q = "SELECT releases.rname";
  q += " FROM card_t,releases,card_ver";
  q += " WHERE card_t.name=='"+r.replace('\'',"''")+"'";
  q +=   " AND card_t.ROWID==card_ver.card";
  q +=   " AND card_ver.ver==releases.rnum;";

  bOK->setEnabled(false);
  lbPrintings->clear();

  for(sqlite3Stmt s(sql,q) ; !s.done() ; s.step()) {
    lbPrintings->insertItem(s.column_text(0));
  }

  lbPrintings->setEnabled(true);

}

/*
 * public slot
 */
void AddCardDialog::printing_selected()
{
  bOK->setEnabled(true);
}

